<?php
namespace Library\Model;

abstract class Model
{
	private $database;
	protected $table;
	protected $primary;
	
	public function __construct($connexionName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function __construct($connexionName)");
		$classConnexion = \Library\Model\Connexion::getInstance();
		$this->database = $classConnexion::getConnexion($connexionName);
	}
	
	public function findByPrimary($valuePrimary, $fields="*")
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function findByPrimary($valuePrimary, $fields='*')");
		$sql = $this->database->prepare("SELECT $fields FROM `{$this->table}` WHERE `{$this->primary}`=:primary");
		$sql->execute(array("primary" => $valuePrimary));
		return $sql->fetchAll();
	}
	
	public function fetchAll($where=1, $field="*")
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function fetchAll($where=1, $field='*')");
		$sql = $this->database->prepare("SELECT $field FROM `{$this->table}` WHERE $where");
		$sql->execute();
		return $sql->fetchAll();
	}
	
	public function insert($data)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function insert($data)");
		$listFields = "`".implode("`, `", array_keys($data))."`";
		$listValues = ":".implode(", :", array_keys($data));
		$sql = $this->database->prepar("INSERT INTO `{$this->table}` ($listFields) VALUES ($listValues)");
		return $sql->execute($data);
	}
	
	public function updateByField($fieldForUpdate, $data)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function updateByField($fieldForUpdate, $data)");
		$list = $this->createListeForUpdate($data, $fieldForUpdate);
		$sql = $this->database->prepar("UPDATE `{$this->table}` SET $list WHERE `$fieldForUpdate` = :$fieldForUpdate");
		return $this->returnAffectedRowBool($sql->execute($data));
	}
	
	public function update($where, $data)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function update($where, $data)");
		$list = $this->createListeForUpdate($data, $fieldForUpdate);
		$sql = $this->database->prepar("UPDATE `{$this->table}` SET $list WHERE $where");
		return $this->returnAffectedRowBool($sql->execute($data));
	}
	
	protected function createListeForUpdate($data, $execlude="")
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> protected function createListeForUpdate($data, $execlude='')");
		$list = "";
		foreach ($data as $key => $value)
		{
			if ($key == $exclude)
				continue;
			$list .= "`$key` = :$key, ";
		}
		return substr($list, 0, -2);
	}
	
	protected function returnAffectedRowBool($query)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> protected function returnAffectedRowBool($query)");
		return ($query && $query->rowCount() > 0);
	}
	
	public function deleteByPrimary($valuePrimary)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function deleteByPrimary($valuePrimary)");
		$sql = $this->database->prepare("DELETE FROM `{$this->table}` WHERE `{$this->primary}`=:primary");
		return $this->returnAffectedRowBool($sql->execute(array("primary" => $valuePrimary)));
	}
	
	public function delete($where)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Model_Model)
			var_dump("namespace Library\Model; class Model -> public function delete($where)");
		$sql = $this->database->prepare("DELETE FROM `{$this->table}` WHERE $where");
		return $this->returnAffectedRowBool($sql->execute());
	}
}

?>